package com.kaluli.project.service;

import java.sql.Timestamp;
import java.util.List;
import java.util.Set;

import javax.transaction.Transactional;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.kaluli.project.domain.entity.KllMainOrder;
import com.kaluli.project.domain.entity.KllMainOrderDetail;
import com.kaluli.project.domain.entity.KllMq;
import com.kaluli.project.domain.entity.KllWareHouse;
import com.kaluli.project.domain.repository.KllMainOrderDetailRepository;
import com.kaluli.project.domain.repository.KllMainOrderRepository;

@Service
@Configuration
@Transactional
@PropertySource({"classpath:thridsystem.properties"})
public class KllMainOrderDetailService {
	
	static Logger log = Logger.getLogger(KllMainOrderDetailService.class);

	@Autowired
	private Environment env;

	@Autowired
	private KllMainOrderRepository kllMainOrderRepository;

	
	@Autowired
	private KllMainOrderDetailRepository kllMainOrderDetailRepository;
	
	@Autowired
	private KllMqService kllMqService;


	public List<KllMainOrder> findAll() {

		return kllMainOrderRepository.findAll();
	}

	public static final String[] MSGCHANNEL = {"ZZ", "GJ", "HG", "ZZ-out"}; // ZZ代表卓志渠道、GJ代表南沙国检渠道、HG代表南沙海关渠道、ZZ-out代表卓志渠道的回调接口

	/**
	 * 将卓志的运单号回写到 订单商品的记录上
	 * 
	 * @param express_no
	 * @param express_name
	 * @param express_note
	 */
	@Transactional(rollbackOn = {RuntimeException.class, Exception.class})
	public int callBackLogistics(String order_number, String express_no, String express_name, String express_note,String requestBody) {
		int returnValue = 1;
		KllMq kllMq = new KllMq();
		kllMq.setMsg_body(requestBody);
		kllMq.setMsg_channel("ZZ-out");
		kllMq.setOrder_number(order_number);
		kllMq.setMsg_status(0);
		kllMq.setMsg_time(new Timestamp(System.currentTimeMillis()));

		KllMainOrder mainOrder = kllMainOrderRepository.findByOrderNumber(order_number);
		Set<KllMainOrderDetail> mainOrderDetails = mainOrder.getKllMainOrderDetails();
		boolean flag = false;
		int index=0;
		for (KllMainOrderDetail orderDetail : mainOrderDetails) {
			KllWareHouse wareHouse = orderDetail.getKllProductSku().getKllWareHouse();
			if(orderDetail.getPayStatus()==1){ //只有当子订单砖塔未订单生成，支付状态为已支付
				if (orderDetail.getDepotType().toString().equalsIgnoreCase(env.getProperty("kll.warehouse.ns"))) { // 目前此程序只用来处理南沙保税商品
					//快递类型
					String type = env.getProperty("zz.domestic_express_type");
					kllMainOrderDetailRepository.updateLogisticsDetail(orderDetail.getId().toString(), express_no, express_name, express_note,type);		
					flag=true;
					index++;
					kllMq.setMsg_status(1);
				}
			}else{
				kllMq.setMsg_response("订单状态不正确");
				returnValue=0;
//				throw new RuntimeException("订单状态异常");
			}
			
		}
		kllMq.setMsg_audit(0);
		kllMq.setMsg_type("logistic");
		kllMqService.save(kllMq);
		return returnValue;

	}
	
	
	/**
	 * 将能容的运单号回写到 订单商品的记录上
	 * 
	 * @param express_no
	 * @param express_name
	 * @param express_note
	 */
	@Transactional(rollbackOn = {RuntimeException.class, Exception.class})
	public int updateEDILogistics(String order_number, String express_no, String express_note,String requestBody,String type) {
		int returnValue = 1;
		KllMq kllMq = new KllMq();
		kllMq.setMsg_body(requestBody);
		kllMq.setMsg_channel("EDI-out");
		kllMq.setOrder_number(order_number);
		kllMq.setMsg_status(0);
		kllMq.setMsg_time(new Timestamp(System.currentTimeMillis()));

		KllMainOrder mainOrder = kllMainOrderRepository.findByOrderNumber(order_number);
		Set<KllMainOrderDetail> mainOrderDetails = mainOrder.getKllMainOrderDetails();
		boolean flag = false;
		int index=0;
		for (KllMainOrderDetail orderDetail : mainOrderDetails) {
			KllWareHouse wareHouse = orderDetail.getKllProductSku().getKllWareHouse();
			if(orderDetail.getPayStatus()==1){ //只有当子订单砖塔未订单生成，支付状态为已支付
				if (orderDetail.getDepotType().toString().equalsIgnoreCase(env.getProperty("kll.warehouse.nb"))) { // 目前此程序只用来处理南沙保税商品
					//快递类型
					
					kllMainOrderDetailRepository.updateLogisticsDetail(orderDetail.getId().toString(), express_no, "", express_note,type);		
					flag=true;
					index++;
					kllMq.setMsg_status(1);
				}
			}else{
				kllMq.setMsg_response("订单状态不正确");
				returnValue=0;
			}
			
		}
		kllMq.setMsg_audit(0);
		kllMq.setMsg_type("logistic");
		kllMqService.save(kllMq);
		return returnValue;

	}
	
	public void updateEDILogistics(String order_number) {
		KllMainOrder mainOrder = kllMainOrderRepository.findByOrderNumber(order_number);
		Set<KllMainOrderDetail> mainOrderDetails = mainOrder.getKllMainOrderDetails();
		boolean flag = false;
		int index=0;
		for (KllMainOrderDetail orderDetail : mainOrderDetails) {
			KllWareHouse wareHouse = orderDetail.getKllProductSku().getKllWareHouse();
			if(orderDetail.getPayStatus()==1){ //只有当子订单砖塔未订单生成，支付状态为已支付
				//快递类型
				kllMainOrderDetailRepository.updateLogisticsDetails(orderDetail.getId().toString());		
				flag=true;
				index++;
			}else{
				
			}
			
		}
		if(flag== true && index==mainOrderDetails.size()){
			kllMainOrderDetailRepository.updateLogisticsMainOrder(order_number);
		}

	}
	
	
	public List<KllMainOrderDetail> getListByDetail(){
		return kllMainOrderDetailRepository.getListByDetail();
	}
	
	
}
